#!/usr/bin/python
# -*- coding: utf-8 -*-

u"""
Generates the html and pdf documentation in doc/html and doc/pdf
"""

import sys
import os
from os import path

try:
    import epydoc
    import epydoc.cli
except:
    sys.stderr.write(u"epydoc is needed to run this script!")
    sys.exit()

def delete_dir(top):
    # dangerous because if top is '/' then the entire disk would be delted!!
    for root, dirs, files in os.walk(top, topdown=False):
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            os.rmdir(os.path.join(root, name))

#change cwd temporaly
old_cwd = os.getcwd()
PCURDIR = path.abspath(path.dirname(__file__))
os.chdir(PCURDIR)

# delete old files first
top = path.abspath(path.join(PCURDIR, os.pardir, 'doc', 'html'))
delete_dir(top)
top = path.abspath(path.join(PCURDIR, os.pardir, 'doc', 'pdf'))
delete_dir(top)

# update command line options
#sys.argv.append("--debug")
sys.argv.append("--config")
sys.argv.append("html.config")
#sys.argv.append("-v") # verbose
#sys.argv.append("pdf.config")

sys.stdout.write('ARGV: %s%s' % (sys.argv, os.linesep))

# run epydoc
epydoc.cli.cli()

# check doc completless
#print dir()
#del epydoc
#print dir()
#import epydoc
#import epydoc.cli
#reload(epydoc)
#reload(epydoc.cli)
#print dir()
##epydoc --check [-p]
#sys.argv = sys.argv[:1]
##sys.argv.append("--debug")
#sys.argv.append("-v") # verbose
#sys.argv.append("--check")
#sys.argv.append("bipy")
#
#sys.stdout.write('ARGV2: %s%s' % (sys.argv, os.linesep))
#
#epydoc.cli.cli()

# change cwd back
os.chdir(old_cwd)
